import logging
from event import Handler
from event_creator import EventCreator
from reactor import Reactor
from buffer_event import BufferReadEvent, BufferWriteEvent

log = logging

class ReactorHandler(Handler):
    def __init__(self, reactor):
        self.__reactor = reactor

    def reactor(self):
        return self.__reactor

class BufferReadHandler(ReactorHandler):
    def __init__(self, reactor):
        super().__init__(reactor)

    def call(self, event):
        if not isinstance(event, BufferReadEvent):
            log.error(f"BufferReadHandler: callbacke with error  event:{event}")

class BufferWriteHandler(ReactorHandler):
    def __init__(self, reactor):
        super().__init__(reactor)

    def call(self, event):
        if not isinstance(event, BufferWriteEvent):
            log.error(f"BufferWriteHandler: callbacke with error  event:{event}")
            return None

        if event.buffer().is_readable():
            print("ERRRRRRRRRRRRRRRRRRR....................{}".format(event.buffer()))
            be = EventCreator.from_buffer_writable(event.transport(), event.handler(), event.buffer())
            
            self.reactor().register(be)
